package com.zixi.ai.framework.entity.listener;


import com.zixi.ai.framework.entity.BaseEntity;
import com.zixi.ai.framework.entity.ExtBaseEntity;
import com.zixi.ai.framework.utils.SecurityUtils;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;

import java.util.Date;

/**
 * 自定义EntityListener
 *
 * @author wang cheng
 */
public class ExtBaseModelEntityListener {

    /**
     * 自动设置租户ID和创建人创建时间
     */
    @PrePersist
    private void prePersist(ExtBaseEntity model) {
        try {
            if (model.getTenantId() == null) {
                model.setTenantId(SecurityUtils.getTenantId());
            }
            model.setCreateBy(SecurityUtils.getUsername());
            model.setCreateTime(new Date());
        } catch (Exception ignored) {}
    }

    @PreUpdate
    private void preUpdate(BaseEntity model) {
        String name = SecurityUtils.getUsername();
        model.setUpdateBy(SecurityUtils.getUsername());
        model.setUpdateTime(new Date());
    }
}
